package com.ibiaoke.customer.service;

import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.exception.ServiceException;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.common.utils.MD5Utils;
import com.ibiaoke.common.utils.SmsUtil;
import com.ibiaoke.customer.dao.CustomerDao;
import com.ibiaoke.customer.dao.DepositDao;
import com.ibiaoke.customer.dao.ThirdPartLoginDao;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.customer.entity.Deposit;
import com.ibiaoke.customer.entity.ThirdPartLogin;
import com.ibiaoke.customer.entity.vo.CustomerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * Created by Administrator on 2017/6/19.
 */
@Service("customerService")
public class CustomerServiceImpl implements CustomerService{

    public static int MIN_PASSWORD_LENGTH = 6;

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private ThirdPartLoginDao thirdPartLoginDao;

    @Autowired
    private DepositDao depositDao;

    @Override
    public List<Customer> listCustomer(String customerUsername, String customerNickname, String customerMobile, String customerEmail, Date startTime, Date endTime, Page page) {
        //系统用户信息
        List<Customer> customerList = customerDao.listCustomer(customerUsername, customerNickname, customerMobile, customerEmail,startTime,endTime,page);

        List<Integer> customerIds = customerList.stream().map(Customer::getCustomerId).collect(toList());
        //微信登录用户信息
        List<ThirdPartLogin> thirdPartLoginList = thirdPartLoginDao.listThiedPartLogin(customerIds);
        //赋值  Key为UserId Value为微信登录用户信息
        Map<Integer, List<ThirdPartLogin>> thirdPartLoginItemGroup = thirdPartLoginList.stream()
                .collect(Collectors.groupingBy(ThirdPartLogin::getUserId));
        //用户Id把系统用户信息和微信登录用户信息整合在一起
        for (Customer customer : customerList) {
            customer.setThirdPartLoginList(thirdPartLoginItemGroup.get(customer.getCustomerId()));
        }
        //账户余额信息
        List<Deposit> depositList = depositDao.depositList(customerIds);
        Map<Integer, List<Deposit>> listDepositItemGroup = depositList.stream()
                .collect(Collectors.groupingBy(Deposit::getCustomerId));
        for (Customer customer : customerList) {
            customer.setDepositList(listDepositItemGroup.get(customer.getCustomerId()));
        }

        return customerList;
    }

    @Override
    public void editCustomer(CustomerVO customerVO) {
        if(null == customerVO.getCustomerId()) {
            throw new ServiceException("需要会员编号");
        }
        Integer customerId =  customerVO.getCustomerId();
        String nickname = customerVO.getCustomerNickname();
        String customerMobile =customerVO.getCustomerMobile();
        String customerEmail = customerVO.getCustomerEmail();
        int rows = customerDao.updateCustomer(customerId, nickname, customerMobile, customerEmail);
        if(rows < 1) {
            throw new ServiceException("编辑用户资料失败");
        }

    }

    @Override
    public Integer getCustomerId(String mobile) {
        Customer customer = getCustomer(mobile);
        return customer==null?null:customer.getCustomerId();
    }

    @Override
    public Customer getCustomerInfo(Integer customerId) {
        return customerDao.selectByPrimaryKey(customerId);
    }

    @Override
    public Customer getCustomer(String mobile) {
        AssertUtils.notBlank(mobile,"手机号不能为空");
        return customerDao.getCustomer(mobile);
    }

    @Override
    public int editLoginPassword(String customerMobile,String password) {
        AssertUtils.notBlank(customerMobile,"手机号不能为空");
        AssertUtils.notBlank(password, "密码不能为空");
        AssertUtils.requireTrue(password.length() >= MIN_PASSWORD_LENGTH, "密码长度不能小于6");
        Customer customer = new Customer();
        customer.setUserSalt(UUID.randomUUID().toString());
        //密码MD5加密
        customer.setCustomerPassword(MD5Utils.getMD5String(password, customer.getUserSalt()));
        return customerDao.updateCustomerPassword(customerMobile,customer.getCustomerPassword(),customer.getUserSalt());
    }

    @Override
    public int updateLoginPassword(String customerMobile, String password,String newPassword) {
        AssertUtils.notBlank(customerMobile,"手机号不能为空");
        AssertUtils.notBlank(password, "旧密码不能为空");
        AssertUtils.notBlank(newPassword, "新密码不能为空");

        AssertUtils.requireTrue(password.length() >= MIN_PASSWORD_LENGTH, "密码长度不能小于6");
        AssertUtils.requireTrue(newPassword.length() >= MIN_PASSWORD_LENGTH, "密码长度不能小于6");

        AssertUtils.requireTrue(newPassword != password,"新密码与旧密码不能相同");

        Customer customer = new Customer();
        customer.setUserSalt(UUID.randomUUID().toString());
        //密码MD5加密
        customer.setCustomerPassword(MD5Utils.getMD5String(newPassword, customer.getUserSalt()));

        return customerDao.updateCustomerPassword(customerMobile,customer.getCustomerPassword(),customer.getUserSalt());
    }

    @Override
    public int forgetLoginPassword(String code, String newPassword, String mobile) {
        AssertUtils.notBlank(mobile, "手机号码不能为空");
        SmsUtil.validateMobileCode(mobile,code);

        Customer customer = new Customer();
        customer.setUserSalt(UUID.randomUUID().toString());
        //密码MD5加密
        customer.setCustomerPassword(MD5Utils.getMD5String(newPassword, customer.getUserSalt()));
        return customerDao.updateCustomerPassword(mobile,customer.getCustomerPassword(),customer.getUserSalt());
    }

    @Override
    public int updateCustomerInfo(Integer customerId,CustomerVO customerVO) {
        AssertUtils.notNull(customerId,"用户Id不能为空");
        return customerDao.updateCustomerInfo(customerId,customerVO);
    }

    @Override
    public int updateCustomerInfos(Integer customerId, String headImageUrl, String nickName) {

        return customerDao.updateCustomerInfos(customerId,headImageUrl,nickName);
    }

    @Override
    public int editUserMobile(Customer customer, CustomerVO customerVO) {
        AssertUtils.notNull(customer.getCustomerId(),"用户Id不能为空");
        AssertUtils.notBlank(customerVO.getCustomerMobile(),"手机号码不能为空");
        AssertUtils.requireTrue(customer.getCustomerMobile() != customerVO.getCustomerMobile(),"新号码不能与旧号码相同");
        return customerDao.editUserMobile(customer.getCustomerId(),customerVO.getCustomerMobile());
    }
}
